//
//  ChannelPageMV_ViewController.m
//  STL_ Animation
//
//  Created by 孙健 on 16/4/5.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "ChannelPageMV_ViewController.h"
#import "Channel_playerIn_ViewController.h"
#import "STL.h"
#import "ChannelPage_Model.h"
#import "ChannelPageTalkAboutModel.h"
#import "ChannelPage_ConllevtionViewCell.h"
#import "TalkAboutTableViewCell.h"
#import "MusicYue_V_TableViewCell.h"
#import "BigMVViewController.h"
@interface ChannelPageMV_ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate,bigAction>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *aboutArray;
@property (nonatomic, strong) NSMutableArray *mvArrayTitle;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *tableViewThree;
@property (nonatomic, strong) UIView *viewOne;
@property (nonatomic, strong) UIView *viewTwo;
@property (nonatomic, strong) UIView *viewThree;
@property (nonatomic, strong) UILabel *labeldescrition;
@property (nonatomic, strong) UILabel *labelTime;
@property (nonatomic, strong) UILabel *labelPlay;
@property (nonatomic, strong) UILabel *labelPc;
@property (nonatomic, strong) UILabel *labelMove;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) NSInteger cellcount;
@property (nonatomic, strong) STLSliderAndButtons *sliderButton;
@property (nonatomic, strong) UIView *playView;
@property (nonatomic, strong) NSMutableArray *arrayMusic;

@end

@implementation ChannelPageMV_ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.aboutArray = [NSMutableArray arrayWithCapacity:0];
    self.view.backgroundColor = [UIColor blackColor];

    self.mvArrayTitle = [NSMutableArray arrayWithCapacity:0];
    self.arrayMusic = [NSMutableArray arrayWithCapacity:0];
    
    [self createSea];
    [self createAbout];
    [self createMV];
    [self createSegment];
    [self getData];
    

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backVc)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cellColor:) name:@"gg" object:nil];
}

- (void)cellColor:(NSNotification *)sender {
    NSNumber *num = sender.object;
    self.cellcount = num.integerValue;
    [self.tableViewThree reloadData];
    //  self.cellColor 为NSInterger 类型属性
}

#pragma mark - 返回上一个vc
- (void)backVc {
    [self.sliderButton.avPlayer pause];
    [self.sliderButton.timer invalidate];
    self.sliderButton.timer = nil;
    [self.sliderButton.timerHidden invalidate];
    self.sliderButton.timerHidden = nil;
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

#pragma mark - 请求网络数据
- (void)getData {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *string = [NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/show.json?D-A=0&id=%@&relatedVideos=true&supportHtml=false&v=2",self.idName];
    [SJAFNetTool GEtNetWithUrl:string WithBody:nil headerFile:Dictionary response:SJJSON success:^(id result) {
        
        if (result) {
            
            ChannelPage_Model *channelPageModel = [[ChannelPage_Model alloc] initWithDataSource:result];
            
            self.labeldescrition.text = channelPageModel.descriptionName;
            self.labeldescrition.frame = CGRectMake(10, ((self.view.bounds.size.height - 64)/2 - 100)/3 + 110, self.viewOne.bounds.size.width - 20,[self setLabeldescrition]);
            
            [self labelText:channelPageModel.regdate WithLabel:channelPageModel.totalViews.stringValue WithLabelPc:channelPageModel.totalPcViews.stringValue WithLabelMove:channelPageModel.totalMobileViews.stringValue];
            self.dataSource = channelPageModel.artists;
            [self.collectionView reloadData];
            
            
            
            [self.mvArrayTitle addObject:channelPageModel];
            
            
            NSLog(@"相关mv个数%ld",self.mvArrayTitle.count);
            
            for (ChannelPage_Model *model in channelPageModel.relatedVideos) {
                
                [self.mvArrayTitle addObject:model];
            }
            
    }
        [self createPlayView];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        } failure:^(NSError *error) {
        [MBProgressHUD showAlertView:@"" andMessage:@"网络错误"];
    }];
    
    // 相关评论网络请求
    
    NSString *stringAbout = [NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/comment/list.json?D-A=0&offset=0&size=20&v=2&videoId=%@",self.idName];
    [SJAFNetTool GEtNetWithUrl:stringAbout WithBody:nil headerFile:Dictionary response:SJJSON success:^(id result) {
        
        NSArray *array = result[@"comments"];
        for (NSDictionary *dic in array) {
            
            ChannelPageTalkAboutModel *talkAboutModel = [[ChannelPageTalkAboutModel alloc] initWithDataSource:dic];
            [self.aboutArray addObject:talkAboutModel];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

#pragma mark - 创建播放视频的view
- (void)createPlayView {
    
    
    
    self.playView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, (self.view.bounds.size.height - 64)/2 - 95)];
//
   
    
    self.sliderButton = [[STLSliderAndButtons alloc] initWithFrame:self.playView.bounds withIndextPathNUM:self.count withType_OneIsYuedan_TwoIsMV:2 withNSMutableArray:self.mvArrayTitle];
    self.sliderButton.bigDelegate = self;
    NSLog(@"----%@-----", self.mvArrayTitle);
    self.playView.backgroundColor = [UIColor blackColor];
    
    [self.playView addSubview:self.sliderButton];
    [self.view addSubview:self.playView];
}

#pragma mark - 创建segment
- (void)createSegment {
    
     self.seg = [[UISegmentedControl alloc] initWithItems:nil ];
     self.seg.frame = CGRectMake(10, (self.view.bounds.size.height - 64)/2 - 95, self.view.bounds.size.width - 20 , 30);
    [self.seg insertSegmentWithTitle:@"MV描述" atIndex:0 animated:NO];
    [self.seg insertSegmentWithTitle:@"评论" atIndex:1 animated:NO];
    [self.seg insertSegmentWithTitle:@"相关MV" atIndex:2 animated:NO];
    self.seg.selectedSegmentIndex = 0;
    self.seg.backgroundColor = [UIColor clearColor];
    self.seg.tintColor = [UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1];
    [self.seg setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];//设置选中的字体 包括颜色和大小
    [self.seg.layer setCornerRadius:CGRectGetHeight([self.seg bounds])/4];
    self.seg.layer.masksToBounds = YES;
    [self.seg addTarget:self action:@selector(createSegmentJiemian:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.seg];
}


#pragma mark - 创建segment相对应的界面
- (void)createSegmentJiemian:(id)sender {
    
    NSInteger segIndex = [self.seg selectedSegmentIndex];
    if (segIndex == 0) {
        
        [self.view bringSubviewToFront:self.viewOne];
        [self.view sendSubviewToBack:self.viewTwo];
        [self.view sendSubviewToBack:self.viewThree];
    }
    else if (segIndex == 1) {
        
        [self.view bringSubviewToFront:self.viewTwo];
        [self.view sendSubviewToBack:self.viewOne];
        [self.view sendSubviewToBack:self.viewThree];
    }
    
    else if (segIndex == 2) {
        [self.view sendSubviewToBack:self.viewTwo];
        [self.view sendSubviewToBack:self.viewOne];
        [self.view bringSubviewToFront:self.viewThree];
    }
}

#pragma mark - 相关mv界面
- (void)createAbout {
    
    
    self.viewThree.backgroundColor = [UIColor blackColor];
    self.viewThree = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.bounds.size.height - 64)/2 - 60, self.view.bounds.size.width, self.view.bounds.size.height - (self.view.bounds.size.height - 64)/2 + 60 - 64)];
    
    self.tableViewThree = [[UITableView alloc] initWithFrame:self.viewThree.bounds style:UITableViewStylePlain];
    self.tableViewThree.delegate = self;
    self.tableViewThree.dataSource = self;
    
    [self.tableViewThree registerClass:[MusicYue_V_TableViewCell class] forCellReuseIdentifier:@"MusicYue_V_TableViewCell"];
    
    [self.viewThree addSubview:self.tableViewThree];
    [self.view addSubview:self.viewThree];
}


#pragma mark - 评论界面
- (void)createSea {
    
    self.viewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.bounds.size.height - 64)/2 - 60, self.view.bounds.size.width, self.view.bounds.size.height - (self.view.bounds.size.height - 64)/2 + 60 - 64)];
    self.viewTwo.backgroundColor = [UIColor blackColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.viewTwo.bounds style:UITableViewStylePlain];
    
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[TalkAboutTableViewCell class] forCellReuseIdentifier:@"TalkAboutTableViewCell"];
    [self.viewTwo addSubview:self.tableView];
    [self.view addSubview:self.viewTwo];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView) {
        return self.aboutArray.count;
    }else {
        
        return self.mvArrayTitle.count;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TalkAboutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TalkAboutTableViewCell"];
    MusicYue_V_TableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"MusicYue_V_TableViewCell"];
    if (tableView == self.tableView) {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.talkAboutModel = self.aboutArray[indexPath.row];
    cell.backgroundColor = [UIColor blackColor];
        return cell;
    }else {
        #warning bug---3
        cell1.buttonSmall.hidden = YES;
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        cell1.musicModel = self.mvArrayTitle[indexPath.row];
        if (indexPath.row == self.cellcount) {
            cell1.labelTitle.textColor = [UIColor redColor];
        } else {
            cell1.labelTitle.textColor = [UIColor blackColor];
        }
        return cell1;
    }
        
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ( tableView == self.tableView) {
        
        TalkAboutTableViewCell *cell = (TalkAboutTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return [cell cellHeight] + 120;
    }
    return ScreenHeight/8;
}

#pragma mark ---------点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableViewThree) {
       
        self.count = indexPath.row;
        [self.sliderButton removeFromSuperview];
        
        self.sliderButton = [[STLSliderAndButtons alloc] initWithFrame:self.playView.bounds withIndextPathNUM:indexPath.row withType_OneIsYuedan_TwoIsMV:2 withNSMutableArray:self.mvArrayTitle];
        self.sliderButton.bigDelegate = self;
        [self.playView addSubview:self.sliderButton];
       
    }
    

}


#pragma mark - MV描述界面
- (void)createMV {
    
    self.viewOne = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.bounds.size.height - 64)/2 - 60, self.view.bounds.size.width, self.view.bounds.size.height - (self.view.bounds.size.height - 64)/2 + 70)];
    self.viewOne.backgroundColor = [UIColor blackColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width - 50, ((self.view.bounds.size.height - 64)/2 - 100)/3);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.viewOne.bounds.size.width, ((self.view.bounds.size.height - 64)/2 - 100)/3) collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.bounces = YES;//开启回弹效果
    self.collectionView.showsHorizontalScrollIndicator = NO;//关闭水平滑动条
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[ChannelPage_ConllevtionViewCell class] forCellWithReuseIdentifier:@"ChannelPage_ConllevtionViewCell"];
    
    
    
    self.labelTime = [[UILabel alloc] initWithFrame:CGRectMake(10, ((self.view.bounds.size.height - 64)/2 - 100)/3 + 20, self.viewOne.bounds.size.width - 20, 30)];
    self.labelTime.font = [UIFont systemFontOfSize:12];
    
    self.labelPlay = [[UILabel alloc] initWithFrame:CGRectMake(10, ((self.view.bounds.size.height - 64)/2 - 100)/3 + 60, (self.viewOne.bounds.size.width - 20)/3, 30)];
    self.labelPlay.font = [UIFont systemFontOfSize:12];
    
    self.labelPc = [[UILabel alloc] initWithFrame:CGRectMake((self.viewOne.bounds.size.width - 20)/3 + 10, ((self.view.bounds.size.height - 64)/2 - 100)/3 + 60, (self.viewOne.bounds.size.width - 20)/3, 30)];
    self.labelPc.textAlignment = NSTextAlignmentCenter;
    self.labelPc.font = [UIFont systemFontOfSize:12];
    
    self.labelMove = [[UILabel alloc] initWithFrame:CGRectMake((self.viewOne.bounds.size.width - 20)/3 * 2 + 10, ((self.view.bounds.size.height - 64)/2 - 100)/3 + 60, (self.viewOne.bounds.size.width - 20)/3, 30)];
    self.labelMove.textAlignment = NSTextAlignmentCenter;
    self.labelMove.font = [UIFont systemFontOfSize:12];
    
    self.labeldescrition = [[UILabel alloc] init];
    self.labeldescrition.numberOfLines = 0;
    self.labeldescrition.textColor = [UIColor whiteColor];
    self.labeldescrition.font = [UIFont systemFontOfSize:14];
    
    [self.viewOne addSubview:self.labeldescrition];
    [self.viewOne addSubview:self.labelTime];
    [self.viewOne addSubview:self.labelPlay];
    [self.viewOne addSubview:self.labelPc];
    [self.viewOne addSubview:self.labelMove];
    [self.viewOne addSubview:self.collectionView];
    [self.view addSubview:self.viewOne];
}

#pragma mark - 各个label的text赋值
- (void)labelText:(NSString *)labelTime WithLabel:(NSString *)labelPlay WithLabelPc:(NSString *)labelPc WithLabelMove:(NSString *)labelMove{
    //属性字符串  发布时间
    NSString *stringTime = @"发布时间:";
    NSString *stringColor = labelTime;
    NSString *string = [NSString stringWithFormat:@"发布时间:%@",labelTime];
    NSRange range = [string rangeOfString:stringTime];
    NSRange rangeColor = [string rangeOfString:stringColor];
    NSMutableAttributedString *stringEnd = [[NSMutableAttributedString alloc] initWithString:string];
    [stringEnd addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:157/255.0 green:152/255.0 blue:147/255.0 alpha:1] range:range];
    [stringEnd addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:rangeColor];
    self.labelTime.attributedText = stringEnd;
    
    NSString *stringPlay = @"播放总次数:";
    NSString *stringColorplay = labelPlay;
    NSString *stringplay = [NSString stringWithFormat:@"播放总次数:%@",labelPlay];
    NSRange rangeplay = [stringplay rangeOfString:stringPlay];
    NSRange rangeColorplay = [stringplay rangeOfString:stringColorplay];
    NSMutableAttributedString *stringEndplay = [[NSMutableAttributedString alloc] initWithString:stringplay];
    [stringEndplay addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:rangeplay];
    [stringEndplay addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:rangeColorplay];
    self.labelPlay.attributedText = stringEndplay;
    
    NSString *stringPc = @"PC端:";
    NSString *stringColorpc = labelPc;
    NSString *stringpc = [NSString stringWithFormat:@"PC端:%@",labelPc];
    NSRange rangepc = [stringpc rangeOfString:stringPc];
    NSRange rangeColorpc = [stringpc rangeOfString:stringColorpc];
    NSMutableAttributedString *stringEndpc = [[NSMutableAttributedString alloc] initWithString:stringpc];
    [stringEndpc addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:rangepc];
    [stringEndpc addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:rangeColorpc];
    self.labelPc.attributedText = stringEndpc;

    
    NSString *stringMove = @"移动端:";
    NSString *stringColormove = labelMove;
    NSString *stringmove = [NSString stringWithFormat:@"移动端:%@",labelMove];
    NSRange rangemove = [stringmove rangeOfString:stringMove];
    NSRange rangeColormove = [stringmove rangeOfString:stringColormove];
    NSMutableAttributedString *stringEndmove = [[NSMutableAttributedString alloc] initWithString:stringmove];
    [stringEndmove addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:rangemove];
    [stringEndmove addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:rangeColormove];
    self.labelMove.attributedText = stringEndmove;
}


#pragma mark - label自适应高度
- (CGFloat)setLabeldescrition {
    
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:self.labeldescrition.font forKey:NSFontAttributeName];
    // 计算宽高

    CGSize size = [self.labeldescrition.text boundingRectWithSize:CGSizeMake(self.viewOne.bounds.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return size.height;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ChannelPage_ConllevtionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelPage_ConllevtionViewCell" forIndexPath:indexPath];
    cell.artistModel = self.dataSource[indexPath.item];
    return cell;
}

#pragma mark - mv描述的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ChannelPage_artistModel *model = self.dataSource[indexPath.item];
    Channel_playerIn_ViewController *playerInVc = [[Channel_playerIn_ViewController alloc] init];
    playerInVc.artistId = model.artistId;
    [self.navigationController pushViewController:playerInVc animated:YES];
    
}

#pragma mark  --------大屏
- (void)bigOrSmall {
    BigMVViewController *bigVC = [[BigMVViewController alloc] init];
    
    //传入需求的model
    //bigVC.ymvModel = self.yMvModel;
    
    [self presentViewController:bigVC animated:YES completion:^{
    }];
}



@end
